package com.ganbo.user.utils;

import org.apache.commons.lang3.StringUtils;

/**
 * 启动参数设置类
 *
 * @author yinjihuan
 * @create 2017-11-21 15:41
 **/

public class StartCommand {

    public StartCommand(String[] args) {
        Boolean isServerPort = false;
        String serverPort = "";
        if (args != null) {
            for (String arg : args) {
                if (StringUtils.isNotBlank(arg) && arg.startsWith("--server.port")) {
                    isServerPort = true;
                    serverPort = arg;
                    break;
                }
            }
        }
        // 没有指定端口，则随机生成一个可用的端口
        if (!isServerPort) {
            int port = ServerPortUtils.getAvailablePort();
            System.out.println("========================current server.port=" + port + "=========================");
            System.setProperty("server.port", String.valueOf(port));
        } else {
            System.out.println("========================current server.port=" + serverPort.split("=")[1] + "=========================");
            System.setProperty("server.port", serverPort.split("=")[1]);
        }
    }

}
